import simplejson, urllib

GEOCODE_BASE_URL = 'http://maps.googleapis.com/maps/api/geocode/json'

def geocode(address,sensor, **geo_args):
    geo_args.update({
        'address': address,
        'sensor': sensor  
    })

    url = GEOCODE_BASE_URL + '?' + urllib.urlencode(geo_args)
    result = simplejson.load(urllib.urlopen(url))

    lat= simplejson.dumps([s['geometry']['location']['lat'] for s in result['results']])
    lng=simplejson.dumps([ s['geometry']['location']['lng'] for s in result['results']])
    print lat
    print lng
    
    return lat+"\t"+lng

if __name__ == '__main__':

	fread=open('/home/vivek/projects/workspace/visproj3/data/airport.data','r+')
	fwrite=open('/home/vivek/projects/workspace/visproj3/data/airporttrial.data','w')
	for line in fread:
		print line.split('\t')
		airport=line.split('\t')[2]+' '+line.split('\t')[3]
		output=geocode(address=airport,sensor="false")
		fwrite.write(output+"\t"+line)
	fread.close()

	fwrite.close()
		

